package com.yurenmatou.utils;


import com.google.common.io.BaseEncoding;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AU {
    private static final String ENCODING = "UTF-8";
    private static final String AES_ALGORITHM = "AES";
    private static final String CIPHER_PADDING = "AES/ECB/PKCS5Padding";
    private static final String CIPHER_CBC_PADDING = "AES/CBC/PKCS5Padding";
    private static final String IV_SEED = "1234567812345678";

    public static String e(String content, String aesKey) {
        if (StringUtil.isBlank(content)) {
            return null;
        }
        if (StringUtil.isNotBlank(aesKey) && aesKey.length() == 16) {
            try {
                byte[] bytes = aesKey.getBytes(ENCODING);
                SecretKeySpec skeySpec = new SecretKeySpec(bytes, AES_ALGORITHM);
                Cipher cipher = Cipher.getInstance(CIPHER_PADDING);
                cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
                byte[] encrypted = cipher.doFinal(content.getBytes(ENCODING));
                String s1 = BaseEncoding.base64().encode(encrypted);
                return s1;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String d(String content, String aesKey) {
        if (StringUtil.isBlank(content)) {
            return null;
        }
        if (StringUtil.isNotBlank(aesKey) && aesKey.length() == 16) {
            try {
                byte[] bytes = aesKey.getBytes(ENCODING);
                SecretKeySpec skeySpec = new SecretKeySpec(bytes, AES_ALGORITHM);
                Cipher cipher = Cipher.getInstance(CIPHER_PADDING);
                cipher.init(Cipher.DECRYPT_MODE, skeySpec);
                byte[] decodeBase64 =  BaseEncoding.base64().decode(content);
                byte[] decrypted = cipher.doFinal(decodeBase64);
                return new String(decrypted, ENCODING);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    public static String ecbc(String content, String aesKey) {
        if (StringUtil.isBlank(content)) {
            return null;
        }
        if (StringUtil.isNotBlank(aesKey) && aesKey.length() == 16) {
            try {
                byte[] bytes = aesKey.getBytes(ENCODING);
                SecretKeySpec skeySpec = new SecretKeySpec(bytes, AES_ALGORITHM);
                Cipher cipher = Cipher.getInstance(CIPHER_CBC_PADDING);
                IvParameterSpec iv = new IvParameterSpec(IV_SEED.getBytes(ENCODING));
                cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
                byte[] encrypted = cipher.doFinal(content.getBytes(ENCODING));
                return BaseEncoding.base64().encode(encrypted);
//                return org.apache.commons.codec.binary.Base64.encodeBase64String(encrypted);
//                return Base64.getEncoder().encodeToString(encrypted);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static String dcbc(String content, String aesKey) {
        if (StringUtil.isBlank(content)) {
            return null;
        }
        if (StringUtil.isNotBlank(aesKey) && aesKey.length() == 16) {
            try {
                byte[] bytes = aesKey.getBytes(ENCODING);
                SecretKeySpec skeySpec = new SecretKeySpec(bytes, AES_ALGORITHM);
                IvParameterSpec iv = new IvParameterSpec(IV_SEED.getBytes(ENCODING));
                Cipher cipher = Cipher.getInstance(CIPHER_CBC_PADDING);
                cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
//                byte[] decodeBase64 = org.apache.commons.codec.binary.Base64.decodeBase64(content);
                byte[] decodeBase64 = BaseEncoding.base64().decode(content);
//                byte[] decodeBase64 = Base64.getDecoder().decode(content);
                byte[] decrypted = cipher.doFinal(decodeBase64);
                return new String(decrypted, ENCODING);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return null;
    }

}
